var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    wrap_one: true,
    wrap_two: false,
    wrap_three: false,
    ifTrue: true,
    Length: 6, //输入框个数
    //isFocusOne: true,    //聚焦
    ValueOne: '', //输入的内容
    //isFocusTwo: true,    //聚焦
    ValueTwo: '', //输入的内容
    ispassword: true, //是否密文显示 true为密文， false为明文。
    //ValueThree: "",
    phone: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.setData({
      phone: options.phone
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  onFocusOne: function(e) {
    //检查原始密码  是不是正确
    var that = this
    var inputValue = e.detail.value
    that.setData({
      ValueOne: inputValue
    })
    if (inputValue.length >= 6) {
      console.log('输入的支付密码为'+inputValue)
      wx.request({
        method: 'POST',
        url: app.preUrl + 'checkPayPad',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: app.globalData.userId,
          pwdPass: that.data.ValueOne
        },
        success: function(res) {
          console.log('检查成功')
          if (res.data.code == 100) {
            that.setData({
              wrap_one: false,
              wrap_two: true
            })
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  that.setData({
                    ValueOne: ''
                  })
                }
              }
            })
          }
        },
        fail: function(error) {
          console.log(error)
        }
      })
    }
  },

  onTapOne: function(e) {
    var that = this
    that.setData({
      isFocusOne: true
    })
  },
  onFocusTwo: function(e) {
    var that = this
    // console.log(e.detail.value)
    var inputValue = e.detail.value
    that.setData({
      ValueTwo: inputValue
    })
    if (inputValue.length >= 6) {
      that.setData({
        wrap_two: false,
        wrap_three: true
      })
    }
  },

  onTapTwo: function(e) {
    // console.log(e.currentTarget.dataset.index)
    var that = this
    that.setData({
      isFocusTwo: true
    })
  },
  onFocusThree: function(e) {
    var that = this
    // console.log(e.detail.value)
    var inputValue = e.detail.value
    that.setData({
      ValueThree: inputValue
    })
    if (inputValue.length >= 6) {
      that.setData({ ifTrue: false })
    } else {
      that.setData({ ifTrue: true })
    }
  },

  onTapThree: function(e) {
    // console.log(e.currentTarget.dataset.index)
    var that = this
    that.setData({
      isFocusThree: true
    })
  },

  formSubmit: function(e) {
    var that = this
    if (
      that.data.ValueOne == '' ||
      that.data.ValueTwo == '' ||
      that.data.ValueThree == ''
    ) {
      wx.showToast({
        title: '各项信息必填',
        icon: 'none'
      })
      return false
    } else if (
      that.data.ValueOne.length != 6 ||
      that.data.ValueTwo.length != 6 ||
      that.data.ValueThree.length != 6
    ) {
      wx.showToast({
        title: '密码长度有误',
        icon: 'none'
      })
      return false
    } else if (that.data.ValueTwo != that.data.ValueThree) {
      wx.showToast({
        title: '2次输入不一致,请重新输入',
        icon: 'none'
      })
      that.setData({
        ValueThree: '',
        ifTrue: true
      })
      return false
    } else {
      console.log('输入的支付密码为'+that.data.ValueOne +',第二次的为'+that.data.ValueTwo+',第三次的为'+that.data.ValueThree)
      that.setData({ ifTrue: true })
      wx.showLoading({ title: '修改中' })
      wx.request({
        method: 'POST',
        url: app.preUrl + 'setUpdatePay',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: app.globalData.userId,
          pwdPass: that.data.ValueOne,
          password: that.data.ValueTwo
        },
        success: function(res) {
           wx.hideLoading()
          if (res.data.code == 100) {
            wx.showToast({
              title: '密码修改成功',
              icon: 'success',
              duration: 1000
            })
            setTimeout(function() {
              wx.navigateBack({ delta: 1 })
            }, 1000)
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            })
            that.setData({ ifTrue: false })
          }
        },
        fail: function(error) {
          console.log(error)
        }
      })
      // }else{
      //   wx.showModal({
      //     title: '2次密码必填且值相同长度为6',
      //     confirmColor: '#56BDE0',
      //     showCancel: false,
      //   })
    }
  },

  //忘记密码
  goCheckSetPaymentPassword: function() {
    var that = this
    var phone = that.data.phone
    wx.navigateTo({
      url: 'checkSetPaymentPassword?phone=' + phone + '&type=2' //2忘记密码
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
})
